package ptumall.utils;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    boolean success;
    private LocalDateTime timestamp = LocalDateTime.now();


    public Result() {
        this.code = 200;
        this.success = true;
    }

    public Result(T data) {
        this.success = true;
        this.code = 200;
        this.data = data;

    }

    public Result(ResultCodeEnum resultCodeEnum) {
        this.success = false;
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();

    }

    public Result(ResultCodeEnum resultCodeEnum, String message) {
        this.success = false;
        this.code = resultCodeEnum.getCode();
        this.message = message;

    }

    public static <T> Result<T> success() {
        return new Result();
    }

    public static <T> Result<T> success(T data) {
        return new Result(data);
    }

    public static <T> Result<T> failure(ResultCodeEnum resultCodeEnum) {
        return new Result<T>(resultCodeEnum);
    }

    public static <T> Result<T> failure(ResultCodeEnum resultCodeEnum, String message) {
        return new Result<T>(resultCodeEnum, message);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ",code=" + code +
                ",data=" + data +
                ",message='" + message + '\'' +
                "}";
    }
}
